package com.jewel.service.impl;

import com.jewel.mapper.TabArticleMapper;
import com.jewel.mapper.TabBlockMapper;
import com.jewel.model.Block;
import com.jewel.model.IndexArticle;
import com.jewel.pojo.TabArticle;
import com.jewel.pojo.TabArticleExample;
import com.jewel.pojo.TabBlock;
import com.jewel.service.ListService;
import com.jewel.tools.Tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 文章列表模块业务类
 * 
 * @author DELL
 *
 * @since 2017.04.27
 */
@Service
public class ListServiceImpl implements ListService {

    @Autowired
    private TabBlockMapper tabBlockMapper;

    @Autowired
    private TabArticleMapper tabArticleMapper;

    /**
	 * @Title: getListById
	 * @Description：获取指定id版区
	 * 
	 * @param listId
	 * @return Block
	 * @throws：
	 * 
	 */
    @Override
    public Block getListById(int listId) {
        Block block = new Block();

        TabBlock tabBlock = this.tabBlockMapper.selectByPrimaryKey(listId);

        if (!Tools.objectIsNullOrNot(tabBlock)){
            block = new Block();

            block.setBlockId(tabBlock.getBlockId());
            block.setName(tabBlock.getName());
        }

        return block;
    }
    
    
    /**
	 * @Title: getAllArticleByListId
	 * @Description：获取指定版区id所有文章
	 * 
	 * @param listId
	 * @return List<IndexArticle>
	 * @throws：
	 * 
	 */
    @Override
    public List<IndexArticle> getAllArticleByListId(int listId) {
        List<IndexArticle> list = null;

        TabArticleExample example = new TabArticleExample();
        TabArticleExample.Criteria criteria = example.createCriteria();
        criteria.andBlockIdEqualTo(listId);

        List<TabArticle> result = this.tabArticleMapper.selectByExample(example);

        if (!Tools.objectIsNullOrNot(result)){
            list = new ArrayList<IndexArticle>();

            for (TabArticle tabArticle : result){
                IndexArticle article = new IndexArticle();

                article.setArticleId(tabArticle.getArticleId());
                article.setTitle(tabArticle.getTitle());
                article.setDescribes(tabArticle.getDescribes());
                article.setCommentNumber(tabArticle.getCommentNumber());
                article.setDateAdd(tabArticle.getDateAdd());
            }
        }

        return list;
    }
}
